﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KorleBu.Application_Classes;

namespace KorleBu
{
    public partial class Allocation : System.Web.UI.Page
    {
        #region Class State Variables
        DataSet ds = new DataSet();
        Presentation presentation = new Presentation();
        //static int STOREID = GetStoreID();
        static int SUBUNITID;
        #endregion

        ////Jason Vance 10-31-12; Lazy initialize the label found on MasterPage.Master
        //private Label _lblMasterError;
        //private Label lblMasterError
        //{
        //    get
        //    {
        //        if (_lblMasterError == null)
        //            _lblMasterError = (Label)Master.FindControl("lblMasterError");
        //        return _lblMasterError;
        //    }
        //}

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ds = presentation.GetSubUnits();
                ddlSubUnitList.DataSource = ds.Tables[1];
                ddlSubUnitList.DataTextField = "subUnitName";
                ddlSubUnitList.DataValueField = "subUnitID";
                ddlSubUnitList.DataBind();
                ddlSubUnitList.Items.Insert(0, new ListItem("Select", null));
            }
        }

        #region Methods
        protected void gvAllocation_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Fill")
            {
                int index = Convert.ToInt32(e.CommandArgument);

                GridViewRow row = gvAllocation.Rows[index];

                if (((TextBox)row.FindControl("txtFill")).Text == "")
                {
                    //lblMasterError.Text = "Must enter a valid quantity";
                }

                else
                {
                    presentation.AllocateItem(GetStoreID(), SUBUNITID, Convert.ToInt32(row.Cells[0].Text), Convert.ToInt32(((TextBox)row.FindControl("txtFill")).Text));

                    ((TextBox)row.FindControl("txtFill")).Text = "";

                    ds = presentation.GetAllItemsForStoreID(GetStoreID(), SUBUNITID); // Need new proc
                    populateGridviewByName(ds);
                }
            }
        }

        protected void ddlSubUnitList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ddlSubUnitList.SelectedItem.Value))
            {
                SUBUNITID = Convert.ToInt32(ddlSubUnitList.SelectedItem.Value);
                ds = presentation.GetAllItemsForStoreID(GetStoreID(), SUBUNITID);

                //counter any errors if any arise, otherwise populate modify user form fields according to returned results.
                if (ds.Tables.Count > 0) // If the method returned successfully
                {
                    if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                    {
                        //lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString(); // Error message
                    }
                    else
                    {
                        gvAllocation.DataSource = ds.Tables[1];
                        gvAllocation.DataBind();
                    }
                }
            }
            //else
            //    lblMasterError.Text = "Sorry, the system could not find the sub unit."; // If the Data Tier cannot be reached clear out any success or error messages to prepare for drop down selection.
        }
        #endregion

        #region Session Methods
        private int GetStoreID()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getStoreId();
        }
        private string GetSessionID()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getSessionId();
        }
        private bool GetIsAdmin()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsAdmin();
        }
        private bool GetIsGeneralManager()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsGeneralManager();
        }
        private bool GetIsStoreManager()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsStoreManager();
        }
        private bool GetIsStoreKeeper()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsStoreKeeper();
        }
        private bool GetIsSubUnitClerk()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsSubUnitClerk();
        }
        private bool GetIsAccounting()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsAccounting();
        }
        private bool GetIsProcurement()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsProcurement();
        }
        private bool GetIsCEO()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsCEO();
        }
        #endregion

        #region Gridview Controls
        protected void populateGridviewByName(DataSet i)
        {

            if (i.Tables[0].Rows[0][0].ToString() != "")
            {
                string me = ds.Tables[0].Rows[0][0].ToString();
                //lblMasterError.Text = me;

                gvAllocation.DataSource = null;
                gvAllocation.DataBind();
            }
            else
            {
                gvAllocation.DataSource = i.Tables[1];
                gvAllocation.DataBind();
                //lblMasterError.Text = "";
            }
        }
        #endregion
    }
}